#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <list>
#include <cmath>

class Map {
public:
    Map(const sf::Vector2u& screenSize);
    sf::Vector2f getMapSize() const { return mMapSize; }
    sf::Vector2f getMapCenter() const { return mMapCenter; }
    
    void loadDefaultMap();
    void render(sf::RenderWindow& window);
    bool checkCollision(const sf::FloatRect& bounds) const;
    bool handleBulletCollision(const sf::FloatRect& bulletBounds);
    bool isInGrass(const sf::FloatRect& bounds) const;  // 检查是否在草地上
    bool isOnIce(const sf::FloatRect& bounds) const;    // 检查是否在冰块上
    
    void playHitSound();      // 播放击中音效
    void playExplosionSound(); // 播放爆炸音效
    
    static void loadTextures();

private:
    enum class TileType {
        Wall,
        DestructibleWall,
        Grass,
        Ice,
        StrongWall  // 新的强化砖块类型
    };

    struct Wall {
        sf::Sprite sprite;
        TileType type;
        bool active = true;  // 墙体是否存在
        int health = 1;      // 墙体生命值，强化砖块为2，其他为1
        bool damaged = false; // 是否已经受损（用于强化砖块的贴图切换）
    };
    
    void createBorder();
    void createWall(const sf::Vector2f& position, TileType type, bool useFlowerTexture = false);
    bool canPlaceAt(const sf::Vector2f& position, TileType type) const;
    void createGrassArea(const sf::Vector2f& center, int size);
    void createIceArea(const sf::Vector2f& center, int size);
    void createStrongWallArea(const sf::Vector2f& center, int size);
    
    static sf::Texture wallTexture;
    static sf::Texture wallDestructibleTexture;
    static sf::Texture grassTexture;
    static sf::Texture grassFlowerTexture;
    static sf::Texture iceTexture;
    static sf::Texture strongWallTexture;      // 完整的强化砖块贴图
    static sf::Texture damagedStrongWallTexture; // 受损的强化砖块贴图
    static bool texturesLoaded;
    
    std::list<Wall> mWalls;  // 使用list以便于删除元素
    // 墙体尺寸
    static constexpr float WALL_WIDTH = 32.f;
    static constexpr float WALL_HEIGHT = 48.f;  // 矩形墙体，高度大于宽度
    
    // 创建组合墙体的方法
    void createWallGroup(const sf::Vector2f& position, int width, int height, TileType type);
    void createRandomWallArea(const sf::Vector2f& center, int width, int height);
    
    // 音效相关
    sf::SoundBuffer mHitSoundBuffer;
    sf::SoundBuffer mExplosionSoundBuffer;
    sf::Sound mHitSound;
    sf::Sound mExplosionSound;
    void loadSounds();  // 加载音效资源

    // 地图尺寸和位置
    sf::Vector2f mMapSize;    // 地图大小
    sf::Vector2f mMapCenter;  // 地图中心点
};